package day_2022_7_to_8.day48;

/**
 * @author haomin
 * @date 2022/08/19 16:54
 **/
public class Day34_3_Num69_MySqrt {
    class Solution1 {
        public int mySqrt(int x) {
            for(int i = 1;i <= x;i++){
                if(Math.pow(i,2) == x) return i;
                else if(Math.pow(i,2) > x) return i-1;
            }
            return 0;
        }
    }
    class Solution {
        public int mySqrt(int x) {
            int left = 0, right = x, res = -1, mid = 0;
            while(left <= right){
                mid = left + ((right-right)/2);
                if((long)mid * mid <= x){
                    res = mid;
                    left = mid +1;
                }else right = mid - 1;
            }
            return res;
        }
    }
}